package groep3.ssc.coachViews;

import groep3.ssc.controllers.ModelRepository;
import groep3.ssc.controllers.ViewController;
import groep3.ssc.main.Main;
import groep3.ssc.main.R;
import groep3.ssc.model.Race;
import groep3.ssc.model.Scheme;
import groep3.ssc.skaterViews.SkaterViewTemplate;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SkateViewManagerActivity extends ListActivity implements OnCheckedChangeListener, OnItemSelectedListener
{

    private ListView listView;
    public static SkateViewManagerAdapter adapter;
    private ArrayList<CheckBox> checkBoxes = new ArrayList<CheckBox>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.skate_view_manager);
        listView = (ListView) findViewById(android.R.id.list);
        adapter = new SkateViewManagerAdapter(getApplicationContext(), R.layout.skate_view_manager_list_item, Main.skaterViews, this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        switch (buttonView.getId())
        {
            case R.id.checkboxSkaterView1:
                Main.skaterView1 = null;
                break;

            case R.id.checkboxSkaterView2:
                Main.skaterView2 = null;
                break;
        }

        LinearLayout parent = (LinearLayout) buttonView.getParent();

        if (!isChecked)
            return;

        for (CheckBox cb : checkBoxes)
        {
            if (cb == buttonView)
                continue;//is same checkbox

            if (cb.getId() != buttonView.getId())
                continue;//not in same group

            cb.setChecked(false);
        }

        switch (buttonView.getId())
        {
            case R.id.checkboxSkaterView1:
                Main.skaterView1 = (SkaterViewTemplate) buttonView.getTag();
                ((CheckBox) parent.findViewById(R.id.checkboxSkaterView2)).setChecked(false);
                break;

            case R.id.checkboxSkaterView2:
                Main.skaterView2 = (SkaterViewTemplate) buttonView.getTag();
                ((CheckBox) parent.findViewById(R.id.checkboxSkaterView1)).setChecked(false);
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        ModelRepository modelRepository = ModelRepository.getInstance();
        Object item = parent.getItemAtPosition(position);
        if (item instanceof Scheme)
        {
            Scheme s = (Scheme) item;
            Race race = modelRepository.getActiveRace();
            if (race != null)
            {
                race.setTargetSchemeId(s.getId());
            }
            modelRepository.setActiveSchemeId(s.getId());
            ViewController.getInstance().update();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
    }

    public void addCheckBox(CheckBox cb)
    {
        this.checkBoxes.add(cb);
    }
}